package com.bluezone.dec.system.service.impl;

import com.bluezone.dec.common.PageEntity;
import com.bluezone.dec.system.dao.EntRelRectMapper;
import com.bluezone.dec.system.entity.EntRelRect;
import com.bluezone.dec.system.service.EntRelRectService;
import com.bluezone.dec.system.service.RectPlanService;
import com.bluezone.dec.system.vo.EnterpriseRelatedPlanVo;
import com.bluezone.dec.system.vo.EnterpriseVo;
import com.bluezone.dec.utils.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author HHL
 * @description 专项整治计划企业关联服务层实现类
 * @date 2018/10/8 - 14:36
 */
@Service
public class EntRelRectServiceImpl implements EntRelRectService {

    private static Logger log = LoggerFactory.getLogger(EntRelRectServiceImpl.class);

    @Autowired
    private EntRelRectMapper entRelRectMapper;

    @Autowired
    private RectPlanService rectPlanService;

    /**
     * 获取所有企业的名称
     *
     * @return
     */
    @Override
    public List<String> getEnterpriseNameList() {
        return entRelRectMapper.getEnterpriseNameList();
    }

    /**
     * 通过专项整治计划Id查询与其相关的企业id集合
     *
     * @param rectPlanId
     * @return
     */
    @Override
    public List<String> findPlanRelEntIdsByRectPlanId(String rectPlanId) {
        return entRelRectMapper.findPlanRelEntIdsByRectPlanId(rectPlanId);
    }

    /**
     * 通过专项整治计划企id删除专项整治计划企业关联信息
     *
     * @param planId
     * @return
     */
    public int deleteRelationshipByPlanId(String planId) {
        return entRelRectMapper.deleteRelationshipByPlanId(planId);
    }

    /**
     * 通过专项整治计划Id查询与其相关的企业
     *
     * @param rectPlanId
     * @return
     */
    @Override
    public List<EntRelRect> findPlanRelEntByRectPlanId(String rectPlanId) {
        return entRelRectMapper.findPlanRelEntByRectPlanId(rectPlanId);
    }

    /**
     * 修改专项整治计划 / 从专项整治计划企业关联表中搜索出该id对应的关联企业并删除
     *
     * @param enterpriseRelatedPlanVo
     */
    public void deltePlanRelateEnterpeise(EnterpriseRelatedPlanVo enterpriseRelatedPlanVo) {

        int i = 0;
        List<EntRelRect> entRelRects = entRelRectMapper.findPlanRelEntByRectPlanId(enterpriseRelatedPlanVo.getRectPlan().getId());

        if (!StringUtils.isEmpty(entRelRects)) {
            for (i = 0; i < entRelRects.size(); i++) {
                entRelRectMapper.deleteRelationshipByPlanId(enterpriseRelatedPlanVo.getRectPlan().getId());
            }
        }
    }

    /**
     * 修改专项整治计划 / 插入修改的计划与企业关联关系
     *
     * @param enterpriseRelatedPlanVo
     */
    public void saveChang4PlanRelateEnterpeise(EnterpriseRelatedPlanVo enterpriseRelatedPlanVo) {
        Assert.notNull(enterpriseRelatedPlanVo, "enterpriseRelatedPlanVo require!!");
        if (!CollectionUtils.isEmpty(enterpriseRelatedPlanVo.getEnterpriseIds())) {
            /* 从Redis缓存查询出所有企业信息 */
            List<EnterpriseVo> enterpriseList = rectPlanService.getEnterpriseListFromRedis();
            try {
                for (String entId : enterpriseRelatedPlanVo.getEnterpriseIds()) {
                    EnterpriseVo enterprise = enterpriseList.stream()
                            .filter(enterpriseVo -> entId.equals(enterpriseVo.getEntCode()))
                            .findAny()
                            .orElse(null);
                    if (enterprise != null) {
                        /* 创建计划与企业关联对象 */
                        EntRelRect entRelRect = new EntRelRect();
                        entRelRect.setId(UUIDUtils.randomUUID());
                        entRelRect.setPlanId(enterpriseRelatedPlanVo.getRectPlan().getId()); // 专项整治计划id
                        entRelRect.setEntCode(entId);
                        entRelRect.setUpdateTime(new Date());
                        entRelRect.setEntName(enterprise.getEntName());
                        entRelRectMapper.insertSelective(entRelRect);
                    }
                }
            } catch (Exception e) {
                log.error("保存计划与企业关联关系失败");
            }
        }
    }

    @Override
    public int save(EntRelRect entRelRect) {
        return 0;
    }

    @Override
    public int update(EntRelRect entRelRect) {
        return 0;
    }

    @Override
    public EntRelRect get(String id) {
        return null;
    }

    @Override
    public int remove(String id) {
        return 0;
    }

    @Override
    public int batchRemove(String... ids) {
        return 0;
    }

    @Override
    public PageEntity<EntRelRect> findByParams(Map<String, Object> params) {
        return null;
    }

    @Override
    public List<EntRelRect> findAll() {
        return new ArrayList<>();
    }

}
